import { ArgumentMetadata, PipeTransform } from "@nestjs/common";

export class ParseJsonPipe implements PipeTransform {
    transform(value: any, metadata: ArgumentMetadata) {
        debugger;
        if (typeof value === "string") {
            const json = this.tryParse(value);
            return json ? json : value;
        }
        return value;
    }

    private tryParse(str: string) {
        try {
            return JSON.parse(str);
        } catch (e) {
            return false;
        }
    }
}
